/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.Group;
import etcore.webapp.grouptalk.common.object.GroupDirectoryRelationship;
import etcore.webapp.grouptalk.common.object.Topic;
import etcore.webapp.grouptalk.user.dao.MemberDAO;
import etcore.webapp.grouptalk.user.object.Member;

/**
 * An instance of GroupDAO is a data access object used to access Group, which
 * contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class GroupDAO extends BaseDAO {

	@Override
	public void deleteSingle(Object persistentInstance) {
		if (persistentInstance instanceof Group) {
			Integer tempid = ((Group) persistentInstance).getId();

			TopicDAO topicDAO = new TopicDAO();
			List<Topic> topics = topicDAO.findByProperty("gid", tempid);
			topicDAO.deleteSingle(topics);

			MemberDAO memberDAO = new MemberDAO();
			List<Member> members = memberDAO.findByProperty("gid", tempid);
			memberDAO.deleteList(members);

			GroupDirectoryRelationshipDAO groupDirectoryRelationshipDAO = new GroupDirectoryRelationshipDAO();
			List<GroupDirectoryRelationship> groupDirectoryRelationships = groupDirectoryRelationshipDAO
					.findByProperty("gid", tempid);
			groupDirectoryRelationshipDAO
					.deleteList(groupDirectoryRelationships);
		}
		super.deleteSingle(persistentInstance);

	}

	/**
	 * Find Group instances
	 * 
	 * @return Group instances
	 */
	public List<Group> findAll() {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group";
		Query queryObject = session.createQuery(queryString);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	/**
	 * Find Group instances
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Group instances
	 */
	public List<Group> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	/**
	 * Find the count of Group instances
	 * 
	 * @return the count of Group instances
	 */
	public long findAllSize() {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group";
		Query queryObject = session.createQuery(queryString);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	/**
	 * Find Group instances by two properties values
	 * 
	 * @param propertyName1
	 *            property name 1
	 * @param value1
	 *            property value 1
	 * @param propertyName2
	 *            property name 2
	 * @param value2
	 *            property value 2
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Group instances
	 */
	public List<Group> findBy2Properties(String propertyName1, Object value1,
			String propertyName2, Object value2, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as model where model." + propertyName1
				+ "= ? and " + propertyName2 + "= ? order by model.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value1);
		queryObject.setParameter(1, value2);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	/**
	 * Find Group instances by values of three properties
	 * 
	 * @param propertyName1
	 *            property name 1
	 * @param value1
	 *            property value 1
	 * @param propertyName2
	 *            property name 2
	 * @param value2
	 *            property value 2
	 * @param propertyName3
	 *            property name 3
	 * @param value3
	 *            property value 3
	 * @param begin
	 *            start position
	 * @param length
	 *            the count of instances
	 * @return Group instances
	 */
	public List<Group> findBy3Properties(String propertyName1, Object value1,
			String propertyName2, Object value2, String propertyName3,
			Object value3, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as model where model." + propertyName1
				+ "= ? and " + propertyName2 + "= ? and " + propertyName3
				+ "=? order by model.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value1);
		queryObject.setParameter(1, value2);
		queryObject.setParameter(2, value3);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	/**
	 * Find Group instance by id
	 * 
	 * @param id
	 *            group id
	 * @return Group instance
	 */
	public Group findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		Group instance = (Group) session.get(Group.class.getName(), id);

		return instance;

	}

	/**
	 * Find Group by name
	 * 
	 * @param name
	 *            unique group name
	 * @return Group instance
	 */
	public Group findByName(String name) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = " from Group where name= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, name);
		@SuppressWarnings("rawtypes")
		List results = queryObject.list();

		if (results != null && results.size() > 0)
			return (Group) results.get(0);
		else
			return null;

	}

	/**
	 * Find Group instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Group instances
	 */

	public List<Group> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as model where model." + propertyName
				+ "= ? order by model.up  ";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findEnglishGroupsByDid(Integer did) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where en=1 and id  in (select distinct gid from GroupDirectoryRelationship where did=? ) order by up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, did);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findEnglishGroupsByNotInDirectories(Integer open,
			Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as groupmodel where groupmodel.en=1 and groupmodel.open=? and groupmodel.id not in (select distinct relationmodel.gid from GroupDirectoryRelationship as relationmodel where relationmodel.did in (select dirmodel.id from Directory as dirmodel where dirmodel.parent=?) ) order by groupmodel.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, parent);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findEnglishGroupsByNotInDirectories(Integer open,
			Integer nav, Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as groupmodel where groupmodel.en=1 and groupmodel.open=? and groupmodel.nav=? and groupmodel.id not in (select distinct relationmodel.gid from GroupDirectoryRelationship as relationmodel where relationmodel.did in (select dirmodel.id from Directory as dirmodel where dirmodel.parent=?) ) order by groupmodel.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, nav);
		queryObject.setParameter(2, parent);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findFavouriteGroups(String userName, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id in (select gid from Member where userName=? and mytype='member' and locked=0)";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, userName);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	@SuppressWarnings("rawtypes")
	public List findActiveGroupIDs(Integer open, Integer nav, Integer days,
			boolean local, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString=null;
		
		if(local)
		queryString = "SELECT gt_group.id  FROM gt_group,gt_topic,gt_message WHERE gt_group.open=? and gt_group.nav=? and gt_group.local=1 AND gt_group.id=gt_topic.gid AND gt_topic.id=gt_message.tid AND (TO_DAYS(NOW())-TO_DAYS(gt_message.addtime))>=0 AND (TO_DAYS(NOW())-TO_DAYS(gt_message.addtime))<=?  GROUP BY gt_group.id ORDER BY gt_group.up ,COUNT(gt_group.id) desc ";
		else 
			queryString = "SELECT gt_group.id  FROM gt_group,gt_topic,gt_message WHERE gt_group.open=? and gt_group.nav=? and gt_group.en=1 AND gt_group.id=gt_topic.gid AND gt_topic.id=gt_message.tid AND (TO_DAYS(NOW())-TO_DAYS(gt_message.addtime))>=0 AND (TO_DAYS(NOW())-TO_DAYS(gt_message.addtime))<=?  GROUP BY gt_group.id ORDER BY gt_group.up ,COUNT(gt_group.id) desc ";
		Query queryObject = session.createSQLQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, nav);
		queryObject.setParameter(2, days);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);

		List results = queryObject.list();

		return results;

	}

	public List<Group> findFavouriteGroups(String userName, int open,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where open=? and id in (select gid from Member where userName=? and mytype='member' and locked=0)";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, userName);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public long findFavouriteGroupsSize(String userName) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where id in (select gid from Member where userName=? and mytype='member' and locked=0 )";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, userName);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findFavouriteGroupsSize(String userName, Integer open) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where open= ? and id in (select gid from Member where userName=?  and ((mytype='member' and locked=0) or mytype='administrator' or mytype='owner')))";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, userName);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public List<Group> findFollowingGroups(String userName, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id in (select gid from Member where userName=? and ((mytype='member' and locked=0) or mytype='administrator' or mytype='owner'))";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, userName);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findFollowingGroups(String userName, int open,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where open=? and id in (select gid from Member where userName=? and ((mytype='member' and locked=0) or mytype='administrator' or mytype='owner'))";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, userName);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public long findFollowingGroupsCount(String userName) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where id in (select gid from Member where userName=? and ((mytype='member' and locked=0) or mytype='administrator' or mytype='owner'))";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, userName);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findFollowingGroupsCount(String userName, int open) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where open=? and id in (select gid from Member where userName=? and ((mytype='member' and locked=0) or mytype='administrator' or mytype='owner'))";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, userName);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public Group findGroupByGmid(Integer gmid) {
		MessageDAO gmdao = new MessageDAO();
		Integer tid = gmdao.findTid(gmid);
		if (tid == null)
			return null;
		TopicDAO gtdao = new TopicDAO();
		Integer gid = gtdao.findGroupId(tid);
		if (gid == null)
			return null;
		return findById(gid);
	}

	public List<Group> findGroupsByDid(Integer did) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id  in (select distinct gid from GroupDirectoryRelationship where did=? ) order by up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, did);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findGroupsByDid(Integer did, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id  in (select distinct gid from GroupDirectoryRelationship where did=? ) order by up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, did);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findGroupsByNDid(Integer did, int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id not in (select gid from GroupDirectoryRelationship where did=? ) order by up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, did);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findGroupsByNotInDirectories(Integer open, Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as groupmodel where groupmodel.open=? and groupmodel.id not in (select distinct relationmodel.gid from GroupDirectoryRelationship as relationmodel where relationmodel.did in (select dirmodel.id from Directory as dirmodel where dirmodel.parent=?) ) order by groupmodel.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, parent);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findGroupsByNotInDirectories(Integer open, Integer nav,
			Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as groupmodel where groupmodel.open=? and groupmodel.nav=? and groupmodel.id not in (select distinct relationmodel.gid from GroupDirectoryRelationship as relationmodel where relationmodel.did in (select dirmodel.id from Directory as dirmodel where dirmodel.parent=?) ) order by groupmodel.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, nav);
		queryObject.setParameter(2, parent);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findLocalGroupsByDid(Integer did) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where local=1 and id  in (select distinct gid from GroupDirectoryRelationship where did=? ) order by up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, did);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findLocalGroupsByNotInDirectories(Integer open,
			Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as groupmodel where groupmodel.local=1 and groupmodel.open=? and groupmodel.id not in (select distinct relationmodel.gid from GroupDirectoryRelationship as relationmodel where relationmodel.did in (select dirmodel.id from Directory as dirmodel where dirmodel.parent=?) ) order by groupmodel.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, parent);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findLocalGroupsByNotInDirectories(Integer open,
			Integer nav, Integer parent) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group as groupmodel where groupmodel.local=1 and groupmodel.open=? and groupmodel.nav=? and groupmodel.id not in (select distinct relationmodel.gid from GroupDirectoryRelationship as relationmodel where relationmodel.did in (select dirmodel.id from Directory as dirmodel where dirmodel.parent=?) ) order by groupmodel.up ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, open);
		queryObject.setParameter(1, nav);
		queryObject.setParameter(2, parent);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findNotRelatedGroups(int gid) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id not in (select gid2 from Grelationship where gid1=?) and id!=?";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gid);
		queryObject.setParameter(1, gid);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findNotRelatedGroups(int gid, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id not in (select gid2 from Grelationship where gid1=?) and id!=?";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gid);
		queryObject.setParameter(1, gid);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public long findNotRelatedGroupSize(int gid) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(id) from Group where id not in (select gid2 from Grelationship where gid1=?) and id!=?";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gid);
		queryObject.setParameter(1, gid);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public List<Group> findRelatedGroups(int gid) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id in (select gid2 from Grelationship where gid1=?)";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gid);

		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public List<Group> findRelatedGroups(int gid, int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where id in (select gid2 from Grelationship where gid1=?)";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gid);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public long findRelatedGroupSize(int gid) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(id) from Group where id in (select gid2 from Grelationship where gid1=?)";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, gid);

		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeBy2Properties(String propertyName, Object value,
			String propertyName2, Object value2) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where " + propertyName
				+ "= ? and " + propertyName2 + "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, value2);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeBy3Properties(String propertyName, Object value,
			String propertyName2, Object value2, String propertyName3,
			Object value3) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where " + propertyName
				+ "= ? and " + propertyName2 + "= ? and " + propertyName3
				+ "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		queryObject.setParameter(1, value2);
		queryObject.setParameter(2, value3);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeByDid(Integer did) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where id  in (select gid from GroupDirectoryRelationship where did=? )  ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, did);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeByNDid(Integer did) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where id not in (select gid from GroupDirectoryRelationship where did=? )  ";

		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, did);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long findSizeByProperty(String propertyName, Object value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where " + propertyName
				+ "= ?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, value);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

	public long isUsedName(String name) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where name=?";
		Query queryObject = session.createQuery(queryString);
		queryObject.setParameter(0, name);
		Object re = queryObject.uniqueResult();

		long k = (re == null) ? 0 : ((Number) re).longValue();
		if (k > 0)
			return 1;
		else
			return 0;

	}

	public List<Group> searchByProperty(String propertyName, String value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		String queryString = "from Group where " + propertyName + " like '%"
				+ value + "%'";
		Query queryObject = session.createQuery(queryString);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		@SuppressWarnings("unchecked")
		List<Group> results = queryObject.list();

		return results;

	}

	public long searchSizeByProperty(String propertyName, String value) {

		Session session = HibernateSessionFactory.getSession();
		String queryString = "select count(*) from Group where " + propertyName
				+ " like '%" + value + "%'";
		Query queryObject = session.createQuery(queryString);
		Object results = queryObject.uniqueResult();

		return (results == null) ? 0 : ((Number) results).longValue();

	}

}